package net.pws.oos.web.mobile;

import java.math.BigDecimal;

import net.pws.common.jackson.JsonUtils;
import net.pws.common.security.filter.ClientVersion;
import net.pws.common.security.filter.ClientVersionProvider;
import net.pws.oos.biz.bdmap.LocationData;
import net.pws.oos.biz.service.MapService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/android")
public class AndroidAction {

	private final transient Log logger = LogFactory.getLog(AndroidAction.class);

	@Autowired
	private ClientVersionProvider versionService;

	@Autowired
	private MapService mapService;

	@RequestMapping("/version.json")
	@ResponseBody
	public Object getVersion() {
		try {
			ClientVersion version = versionService.getVersion();
			return JsonUtils.succeedMap(version);
		} catch (Exception e) {
			logger.error(e, e);
			return JsonUtils.failedMap(e.getMessage());
		}
	}

	@RequestMapping("/download.do")
	public String download(ModelMap map) {
		ClientVersion version = versionService.getVersion();
		map.put("path", version.getPath());
		return "../android/download";
	}

	/**
	 * 用于手机端测试的
	 * 
	 * @return
	 */
	@RequestMapping("/test.json")
	@ResponseBody
	public Object test() {
		return JsonUtils.succeedMap(null);
	}

	@RequestMapping("/address.json")
	@ResponseBody
	public Object getAddress(BigDecimal longitude, BigDecimal latitude) {
		LocationData location = null;
		try {
			location = mapService.getAddress(longitude, latitude);
		} catch (Exception e) {
			return JsonUtils.failedMap(e.getMessage());
		}

		if (location == null) {
			return JsonUtils.failedMap("获得位置信息失败！");
		}
		return JsonUtils.succeedMap(location);
	}
}
